var totalSelected = 0;
var uploadingnum = 1;
function fileQueued(file){
	if($('#'+this.customSettings.upload_target).hasClass('hide'))
		$('#'+this.customSettings.upload_target).removeClass('hide');
	//if(!$('#fetchFileProgressContainer').hasClass('hide'))
	//	$('#fetchFileProgressContainer').addClass('hide');
}
function fileQueueError(file, errorCode, message) {
	try {
		var errorName = "";
		if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
			errorName = tip_queue_too_many;
		}

		if (errorName !== "") {
			show_message('error',errorName,false,0);
			return;
		}

		switch (errorCode) {
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			message = tip_file_zero;
			break;
		case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
			message = tip_file_too_big;
			break;
		case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
		default:
			message = tip_file_not_valid;
			break;
		}
		addImageError({message:message},this.customSettings.thumb_target,this.customSettings.thumberror_tpl,this.customSettings.duration);

	} catch (ex) {
		this.debug(ex);
	}

}

function fileDialogComplete(numFilesSelected, numFilesQueued) {
	totalSelected = numFilesSelected;
	uploadingnum = 1;
	try {
		if (numFilesQueued > 0) {
			this.startUpload();
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadProgress(file, bytesLoaded) {
	try {
		var percent = Math.ceil((bytesLoaded / file.size) * 100);

		var progress = new FileProgress(file,  this.customSettings.upload_target);
		progress.setProgress(percent);
		if (percent === 100) {
			progress.setStatus(tip_creating_thumb);
			progress.toggleCancel(false, this);
		} else {
			progress.setStatus(tip_uploading);
			progress.toggleCancel(true, this);
		}
	} catch (ex) {
		this.debug(ex);
	}
}


function addImageError(data,target,tpl,duration) {
	getTpl(tpl,function(){
		var tpl_ins = $('#'+tpl+'_tpl');
		var lis = $(Mustache.to_html(tpl_ins.html(), data));
		$('#'+target).removeClass("hide");
		$(lis).addClass('hide');
		$('#'+target).append(lis);
		$(lis).fadeIn(duration);
	 });
}

/*function addImageData(data,target,thumbimg_tpl,thumbothers_tpl,duration) {
	var tpl = thumbimg_tpl;
	if(data.isimage!=1){
		tpl = thumbothers_tpl;
	}
	getTpl(tpl,function(){
		var tpl_ins = $('#'+tpl+'_tpl');
		var lis = $(Mustache.to_html(tpl_ins.html(), data));
		$('#'+target).removeClass("hide");
		$(lis).addClass('hide');
		$('#'+target).append(lis);
		$(lis).fadeIn(duration);
	 });
}*/

function uploadSuccess(file, serverData) {
	try {
		if(uploadingnum < totalSelected) uploadingnum++;
		var progress = new FileProgress(file,  this.customSettings.upload_target);

		if (serverData.substring(0, 7) === "FILEID:") {
			var arr = serverData.substring(7).split("|");
			var aid = arr[0];
			var fileid = arr[1];
			var apath = base_url + arr[2];
			var filename = arr[3];
			var isimage = arr[4];
			var ext = arr[5];
			var data = {aid:aid,fileid:fileid,apath:apath,filename:filename,isimage:isimage,editorid:this.customSettings.editorid,ext:ext};
			var tpl = this.customSettings.thumbimg_tpl;
			if(data.isimage!=1){
				tpl = this.customSettings.thumbothers_tpl;
			}
			
			addImageData(data,this.customSettings.thumb_target,tpl,false,this.customSettings.duration);
			progress.toggleCancel(false);
		} else {
			addImageError({message:serverData},this.customSettings.thumb_target,this.customSettings.thumberror_tpl,this.customSettings.duration);
			progress.setStatus(tip_error);
			progress.toggleCancel(false);
		}


	} catch (ex) {
		this.debug(ex);
	}
}

function uploadComplete(file) {
	try {
		/*  I want the next upload to continue automatically so I'll call startUpload here */
		if (this.getStats().files_queued > 0) {
			this.startUpload();
		} else {
			var progress = new FileProgress(file,  this.customSettings.upload_target);
			progress.setComplete();
			progress.setStatus(tip_upload_completed);
			progress.toggleCancel(false);
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadError(file, errorCode, message) {
	var progress;
	try {
		switch (errorCode) {
		case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
			try {
				progress = new FileProgress(file,  this.customSettings.upload_target);
				progress.setCancelled();
				progress.setStatus(tip_cancelled);
				progress.toggleCancel(false);
			}
			catch (ex1) {
				this.debug(ex1);
			}
			message = tip_cancelled;
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
			try {
				progress = new FileProgress(file,  this.customSettings.upload_target);
				progress.setCancelled();
				progress.setStatus(tip_stopped);
				progress.toggleCancel(true);
			}
			catch (ex2) {
				this.debug(ex2);
			}
			message = tip_stopped;
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
			message = tip_queue_too_many;
			break;
		default:
			break;
		}

		addImageError({message:message},this.customSettings.thumb_target,this.customSettings.thumberror_tpl,this.customSettings.duration);

	} catch (ex3) {
		this.debug(ex3);
	}

}


/* ******************************************
 *	FileProgress Object
 *	Control object for displaying file info
 * ****************************************** */

function FileProgress(file, targetID) {
	this.fileProgressID = "divFileProgress";

	var s = ""+uploadingnum+"/"+totalSelected;
	this.fileProgressWrapper = document.getElementById(this.fileProgressID);
	if (!this.fileProgressWrapper) {
		this.fileProgressWrapper = document.createElement("div");
		this.fileProgressWrapper.className = "progressWrapper";
		this.fileProgressWrapper.id = this.fileProgressID;

		this.fileProgressElement = document.createElement("div");
		this.fileProgressElement.className = "progressContainer";

		var progressCancel = document.createElement("a");
		progressCancel.className = "progressCancel";
		progressCancel.href = "#";
		progressCancel.style.visibility = "hidden";
		progressCancel.appendChild(document.createTextNode(" "));

		var progressText = document.createElement("div");
		progressText.className = "progressName";
		progressText.appendChild(document.createTextNode(s));
		
		var progressBar = document.createElement("div");
		progressBar.className = "progress progress-striped";

		var progressBarChild = document.createElement("div");
		progressBarChild.className = "bar";
		progressBar.appendChild(progressBarChild);

		var progressStatus = document.createElement("div");
		progressStatus.className = "progressBarStatus";
		progressStatus.innerHTML = "&nbsp;";

		this.fileProgressElement.appendChild(progressCancel);
		this.fileProgressElement.appendChild(progressText);
		this.fileProgressElement.appendChild(progressStatus);
		this.fileProgressElement.appendChild(progressBar);

		this.fileProgressWrapper.appendChild(this.fileProgressElement);

		document.getElementById(targetID).appendChild(this.fileProgressWrapper);
		fadeIn(this.fileProgressWrapper, 0);

	} else {
		
		this.fileProgressElement = this.fileProgressWrapper.firstChild;
		this.fileProgressElement.childNodes[1].firstChild.nodeValue = s;
	}

	this.height = this.fileProgressWrapper.offsetHeight;

}
FileProgress.prototype.setProgress = function (percentage) {
	this.fileProgressElement.className = "progressContainer green";
	this.fileProgressElement.childNodes[3].className = "progress progress-striped active";
	this.fileProgressElement.childNodes[3].childNodes[0].style.width = percentage + "%";
};
FileProgress.prototype.setComplete = function () {
	this.fileProgressElement.className = "progressContainer blue";
	this.fileProgressElement.childNodes[3].className = "progressBarComplete";
	this.fileProgressElement.childNodes[3].childNodes[0].style.width = "";

};
FileProgress.prototype.setError = function () {
	this.fileProgressElement.className = "progressContainer red";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].childNodes[0].style.width = "";

};
FileProgress.prototype.setCancelled = function () {
	this.fileProgressElement.className = "progressContainer";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].childNodes[0].style.width = "";

};
FileProgress.prototype.setStatus = function (status) {
	this.fileProgressElement.childNodes[2].innerHTML = status;
};

FileProgress.prototype.toggleCancel = function (show, swfuploadInstance) {
	this.fileProgressElement.childNodes[0].style.visibility = show ? "visible" : "hidden";
	if (swfuploadInstance) {
		var fileID = this.fileProgressID;
		this.fileProgressElement.childNodes[0].onclick = function () {
			swfuploadInstance.cancelUpload(fileID);
			return false;
		};
	}
};
